function [ para, yFit, rSqr ] = expFit( xData, yData, slope, fitUntilExtremum, fitFromExtremum, xFit )
if fitUntilExtremum
    yDataFilt = medfilt1(yData, 7, 'truncate');
    if slope > 0
        [~,pklocs] = findpeaks(yDataFilt);
        lastExtremumIdx = find(yDataFilt == max(yDataFilt), 1, 'last');
    else
        [~,pklocs] = findpeaks(-yDataFilt);
        lastExtremumIdx = find(yDataFilt == min(yDataFilt), 1, 'last');
    end
    if ~isempty(pklocs)
        if ~any(abs(pklocs - lastExtremumIdx) <= 2)
            lastExtremumIdx = max(pklocs(end),lastExtremumIdx);
        end
    end
    if length(xData) > 6
        lastExtremumIdx = max(lastExtremumIdx,6);
    end
    xData = xData(1:lastExtremumIdx);
    yData = yData(1:lastExtremumIdx);
end
if fitFromExtremum
    yDataFilt = medfilt1(yData(1:end-2), 7);
    if slope > 0
        [~,pklocs] = findpeaks(-yDataFilt);
        lastExtremumIdx = find(yDataFilt == min(yDataFilt), 1, 'last');
    else
        [~,pklocs] = findpeaks(yDataFilt);
        lastExtremumIdx = find(yDataFilt == max(yDataFilt), 1, 'last');
    end
    if ~isempty(pklocs)
        if ~any(abs(pklocs - lastExtremumIdx) <= 2)
            lastExtremumIdx = min(pklocs(end),lastExtremumIdx);
        end
    end
    lastExtremumIdx = min(lastExtremumIdx,length(yData)-5);
    xData = xData(lastExtremumIdx:end);
    yData = yData(lastExtremumIdx:end);
end

para = exp2fit(xData,yData,1);
yFit = para(1)+para(2)*exp(-xFit/para(3));
rSqr = 1 - sum( ( yData - (para(1)+para(2)*exp(-xData/para(3))) ).^2 ) / sum((yData - mean(yData)).^2);
end